package com.zzh.netty.nettyServer;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @PackageName:com.zzh.netty.nettyServer
 * @ClassName:NettyClient
 * @Description: netty客户端
 * @author:zhangzhanheng
 * @date: 2021/1/30 14:13
 */
public class NettyClient {

    public static void main(String[] args) {
        EventLoopGroup group = new NioEventLoopGroup();

        try {
          Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new NettyClientHandler());
                        }
                    });
            System.out.println("netty client start....");
            ChannelFuture cf = bootstrap.connect("127.0.0.1",9000).sync();
            cf.channel().closeFuture().sync();

        } catch (Exception e) {

        }finally {
            group.shutdownGracefully();
        }
    }
}
